import os,sys
import potionUtil
import socketUtil
import cmdUtil
import cvUtil
from queue import Queue
import time

class DPoint(object):
    """docstring for Stat"""
    def __init__(self,px,py,pz):
        self.p = (px,py)
        self.z = pz
    def getPoint(self):
        return self.p
    def moveby(self,pt):
        self.p = potionUtil.add(self.p,pt)
    def moveTo(self,pt):
        self.p = p
    def showPoint(self):
        print('DP:',self.p)

DPobj = DPoint(0,0,0)

DSIZE = (84.0,63.0)
OFFSET = (5,50,22)

IMGSIZE = None

SOCKETQUEUE = Queue()

def dpmove(x,y):
    DPobj.moveby((x,y))
    cmdUtil.moveTo(SOCKETQUEUE,DPobj.p[0],DPobj.p[1])

def onImgSizeChange(psize):
    global IMGSIZE
    IMGSIZE = psize

def onTouchImg(x,y):
    print('ip',(x,y))
    pobj = potionUtil.Point(x,y,(IMGSIZE[0]/2,IMGSIZE[1]/2),(DSIZE[0]/IMGSIZE[0],DSIZE[1]/IMGSIZE[1]))
    mp = pobj.getMP()
    print('mp:',mp)
    mdp = pobj.getMDP()
    print('mdp:',mdp)
    dp = pobj.getDP(DPobj.p)
    print('dp:',dp)
    dpmove(mdp[0],mdp[1])

MOVEPSTEP = 1
LIGHTSTATE = False

def onKeyDown(key):
    global MOVEPSTEP
    global LIGHTSTATE
    if key & 0xFF == 0x1B:
        exit(0)                                 
    if key & 0xFF == 0x20:
        print('space down')
        dpmove(OFFSET[0],OFFSET[1])
        cmdUtil.touchOnce(SOCKETQUEUE,OFFSET[2])
        dpmove(-OFFSET[0],-OFFSET[1])
    else:
        print('on key down:%x'%(key))
        pt = (0,0)
        if key & 0xFF == ord('h'):##归零操作
            print('on key down:%s'%(chr(key)))
            cmdUtil.homing(SOCKETQUEUE)
            return
        elif (key & 0xFF == 0x0D):##回车,显示当前坐标
            DPobj.showPoint()
        elif (key & 0xFF == ord('l')):
            LIGHTSTATE = not LIGHTSTATE
            cmdUtil.setLight(SOCKETQUEUE,LIGHTSTATE)
        elif (key & 0xFF == ord('w')):##上
            dpmove(0,-MOVEPSTEP)
        elif (key & 0xFF == ord('s')):##s,下
            dpmove(0,MOVEPSTEP)
        elif (key & 0xFF == ord('a')):##a,左
            dpmove(-MOVEPSTEP,0)
        elif (key & 0xFF == ord('d')):##d,右
            dpmove(MOVEPSTEP,0)
        elif key & 0xFF == ord(',') or key & 0xFF == ord('<'):
            if MOVEPSTEP > 1:
                MOVEPSTEP -= 1 
            print('move step:',MOVEPSTEP)
        elif key & 0xFF == ord('.') or key & 0xFF == ord('>'):
            if MOVEPSTEP < 50:
                MOVEPSTEP += 1 
            print('move step:',MOVEPSTEP)
        elif key & 0xFF >= ord('1') and key & 0xFF <= ord('9'):#1键
            MOVEPSTEP = (key & 0xFF) - ord('0')
            print('move step:',MOVEPSTEP)
        elif key == 9:#tab键
            MOVEPSTEP = 1
            print('move step:',MOVEPSTEP)

DEVICEIPADDR = '192.168.8.130'

def main():
    net_thread = socketUtil.socketThread(SOCKETQUEUE,DEVICEIPADDR)
    net_thread.setDaemon(True)
    net_thread.start()
    time.sleep(1)
    cmdUtil.homing(SOCKETQUEUE)
    # 'http://192.168.8.130:81/stream'
    devurl = 'http://' + DEVICEIPADDR + ':81/stream'
    imgobj = cvUtil.cv2Stream(SOCKETQUEUE,devurl,'capimg.jpg',onTouchImg,onKeyDown,onImgSizeChange)
    imgobj.start()

if __name__ == '__main__':  
    main()
    